﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private Rigidbody2D rb;
    public Animator anim;

    //private deathDelegate handlerDebugString;


    [Header("移动参数")]
    public float speed = 8f;

    [Header("跳跃参数")]
    public float jumpForce = 6.3f;
    public float dropForce;

    int jumpCount;

    [Header("状态")]
    public bool isJump;
    public bool isOnGround;
    public bool filp;
    [SerializeField]
    private bool isDrop;

    [Header("环境检测")]
    public float footOffset = 0.4f;//地面两检测射线之间的间距
    public float groundDistance = 0.2f;//地面检测射线长度
    public float y = 0.1f;//地面检测射线高度

    [Header("下砸参数")]
    public float addDropForce;//瞬时力大小

    [Header("踩敌检测器")]
    public LayerMask whatIsEnemies;
    public Vector3 pos;
    public Vector2 AttackRange;
    private float Angle;
    private float MinDepth;
    private float MaxDepth;

    [Header("地面射线检测图层")]
    public LayerMask groundLayer;
    [Header("左右按键测试量")]
    public float xVelocity;

    //按键设置
    bool jumpPressed;

    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
        //anim = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        PhysicsCheck();
        //按下下砸
        if (!isOnGround&&Input.GetKeyDown(KeyCode.LeftShift)&&!isDrop)
        {
            //Debug.Log("success");
            rb.AddForce(new Vector2(0, addDropForce), ForceMode2D.Impulse);
            isDrop = true;
        }
        if (isOnGround)
        {
            isDrop = false;
        }
        if (isDrop&&!isOnGround)
        {
            rb.velocity = new Vector2(0,rb.velocity.y);
            return;
        }

        //跳跃
        if (Input.GetButtonDown("Jump") && jumpCount > 0)//限制掉落平台后可以空中借力多次连跳
        {
            jumpPressed = Input.GetButtonDown("Jump");
        }
        //调用移动
        GroundMovement();
        MidAirMovement();

        //if (!isOnGround&&rb.velocity.y<=0)//改变下降速度
        //{
        //    rb.velocity = new Vector2(rb.velocity.x, -dropForce);
        //}
    }

    void PhysicsCheck()
    {
        //地面判断
        RaycastHit2D leftCheck = Raycast(new Vector2(-footOffset, y), Vector2.down, groundDistance, groundLayer);
        RaycastHit2D rightCheck = Raycast(new Vector2(footOffset, y), Vector2.down, groundDistance, groundLayer);

        //敌人判断
        if (rb.velocity.y<0)
        {
            Collider2D enemy = Physics2D.OverlapBox(transform.position + pos, AttackRange, Angle, whatIsEnemies, MinDepth, MaxDepth);

            if (enemy)
            {
                Debug.Log(1);
                enemy.GetComponent<EnemyBase>().Die();
            }
        }
        

        //左脚右脚射线检测
        if (leftCheck || rightCheck)
        {
            isOnGround = true;
        }
        else
        {
            isOnGround = false;
        }
    }
    /// <summary>
    /// 固定帧刷新，移动写在这里
    /// </summary>
    void FixedUpdate()
    {

    }
    //人物移动
    void GroundMovement()
    {
        FilpDirction();
        xVelocity = Input.GetAxisRaw("Horizontal");
        rb.velocity = new Vector2(xVelocity * speed, rb.velocity.y);

        anim.SetFloat("Run", Mathf.Abs(xVelocity));
    }

    void MidAirMovement()
    {
        //判定地面和跳跃次数
        if (isOnGround)
        {
            jumpCount = 1;
            isJump = false;
        }
        if (jumpPressed && isOnGround)
        {

            isJump = true;
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            jumpCount--;
            jumpPressed = false;
        }
        else if (jumpPressed && jumpCount >= 0 && !isOnGround)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
            jumpCount--;
            jumpPressed = false;
        }
    }

    void FilpDirction()
    {
        //转向
        if (xVelocity < 0)
        {
            transform.localScale = new Vector3(-1, 1, 1);
        }

        if (xVelocity > 0)
        {
            transform.localScale = new Vector3(1, 1, 1);
        }
    }

    //重载射线
    RaycastHit2D Raycast(Vector2 offset, Vector2 rayDiraction, float length, LayerMask layer)
    {
        Vector2 pos = transform.position;
        RaycastHit2D hit = Physics2D.Raycast(pos + offset, rayDiraction, length, layer);
        Color color = hit ? Color.green : Color.red;
        Debug.DrawRay(pos + offset, rayDiraction * length, color);
        return hit;
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireCube(transform.position + pos, AttackRange);//Cube
    }
}
